package com.javasea.netty.tcp.simpleclient;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;


//@ChannelHandler.Sharable
public class HeartClientHeartBeat extends SimpleChannelInboundHandler<String> {

    private String deviceId;

    public HeartClientHeartBeat(String hostPort) {
        this.deviceId = hostPort;
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent idleEvent = (IdleStateEvent) evt;
            if (idleEvent.state() == IdleState.READER_IDLE) {
            } else if (idleEvent.state() == IdleState.WRITER_IDLE) {
                System.out.println("hello");
                ctx.channel().writeAndFlush("hello");
            } else if (idleEvent.state() == IdleState.ALL_IDLE) {
            }
        }
        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(11111);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String msg ) throws Exception {
        System.out.println(deviceId + " >> channelRead >> "+ msg);
    }
}
